package com.banana.linkedlist;

import com.sun.xml.internal.bind.v2.model.core.ID;
import org.junit.jupiter.api.Test;

/**
 * 双向链表
 * 注意：双链表除了维护next指针，还维护了pre指针，在作对节点的增加/删除时，注意同时维护next和pre
 */
public class DoubleLinkedListDemo {
    public static DoubleLinkedList getOneDoubleLinkedList() {
        // 测试
        DoubleHeroNode hero1 = new DoubleHeroNode(1,"宋江","及时雨");
        DoubleHeroNode hero2 = new DoubleHeroNode(2,"卢俊义","玉麒麟");
        DoubleHeroNode hero3 = new DoubleHeroNode(3,"吴用","智多星");
        DoubleHeroNode hero4 = new DoubleHeroNode(4,"林冲","豹子头");

        DoubleLinkedList list = new DoubleLinkedList();
        list.addNewNode(hero1);
        list.addNewNode(hero2);
        list.addNewNode(hero3);
        list.addNewNode(hero4);
        return list;
    }

    @Test
    void testCrud() {
        DoubleLinkedList list = getOneDoubleLinkedList();
        list.list();
        System.out.println("添加节点：");
        DoubleHeroNode hero5 = new DoubleHeroNode(5,"李逵","黑旋风");
        list.addNewNode(hero5);
        list.list();
        System.out.println("修改节点：");
        DoubleHeroNode hero3 = new DoubleHeroNode(3,"吴用~~","智多星~~");
        DoubleHeroNode hero999 = new DoubleHeroNode(999,"吴用~~","智多星~~");
        list.updateByNode(hero3);
        list.updateByNode(hero999);
        list.list();
        System.out.println("删除节点：");
        list.deleteNode(hero999);
        list.deleteNode(hero5);
        list.deleteNodeById(1);
        list.deleteNodeById(3);
        list.list();

    }

}

class DoubleLinkedList {
    public final DoubleHeroNode head = new DoubleHeroNode(0,"","");

    /**
     * 根据id删除对应的节点
     * @param id 待删除节点
     */
    public void deleteNodeById(int id) {
        if (size() == 0) {
            return;
        }
        DoubleHeroNode temp = head;
        boolean existFlag = false;
        while (temp.next != null){
            if (temp.id == id) {
                // 找到了
                existFlag = true;
                temp.pre.next = temp.next;
                temp.next.pre = temp.pre;
                break;
            }
            temp = temp.next;
        }
        // 判断是否删尾节点
        if (!existFlag && temp.id == id) {
            // 找到了
            existFlag = true;
            temp.pre.next = null;
        }
        if (!existFlag) {
            System.out.printf("双链表中没有id为%d的节点\n",id);
        }
    }

    public void deleteNode(DoubleHeroNode node) {
        deleteNodeById(node.id);
    }

    /**
     * 根据id更新链表
     * @param node 节点
     */
    public void updateByNode(DoubleHeroNode node) {
        if (size() == 0) {
            System.out.println("双链表长度为0");
            return;
        }
        DoubleHeroNode temp = head;
        boolean existFlag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.id == node.id) {
                existFlag = true;
                // 修改节点
                temp.name = node.name;
                temp.nickName = node.nickName;
                break;
            }
            // 继续后移
            temp = temp.next;
        }
        if (!existFlag) {
            System.out.println("双链表中没有这个节点：" + node.toString());
        }
    }

    /**
     * 添加一个新节点到双链表尾部
     * @param newNode 新节点
     */
    public void addNewNode(DoubleHeroNode newNode) {
        DoubleHeroNode temp = head;
        while (temp.next != null) {
            if (temp.id == newNode.id) {
                System.out.printf("添加新节点失败，id为%d的节点已经存在：" + temp.toString(),newNode.id);
            }
            temp = temp.next;
        }
        // 尾部节点的next指向新节点，新节点的pre指向尾部节点
        temp.next = newNode;
        newNode.pre = temp;
    }

    public DoubleHeroNode getHead() {
        return head;
    }

    public int size() {
        DoubleHeroNode temp = head.next;
        int count = 0;
        if (temp != null) {
            while (temp.next != null) {
                count++;
                temp = temp.next;
            }
        }
        return count;
    }

    public void list() {
        // 1.判断长度
        if (size() == 0) {
            System.out.println(head);
            return;
        }
        // 2.遍历
        DoubleHeroNode temp = head.next;
        while (temp.next != null) {
            System.out.println(temp);
            temp = temp.next;
        }
        System.out.println(temp);
    }

}

class DoubleHeroNode {
    int id;
    String name;
    String nickName;
    DoubleHeroNode pre;
    DoubleHeroNode next;

    public DoubleHeroNode(int id, String name, String nickName) {
        this.id = id;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "DoubleHeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}
